<template>
  <div class="app-container">
    <el-form :model="dataForm" class="cus_form">
      <el-row :gutter="20">
        <el-col :span="14">
          <el-form-item>
            <el-input v-model="dataForm.IndexCondition" size="mini" placeholder="名称/代码" clearable>
              <el-button slot="append" icon="el-icon-search" @click="getDataList()" />
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" style="text-align: right; ">
          <el-button icon="el-icon-plus" size="mini" class="cla-user-add" type="primary" @click="addOrUpdateHandle()">
            新建
          </el-button>
            &nbsp;&nbsp;
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      :data="dataList"
      class="eltable"
      :height="viewHeight"
      style="width: 100%;"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" header-align="left" align="left" width="30" />
      <el-table-column v-if="false" prop="userId" header-align="left" align="left" width="80" label="ID" />
      <el-table-column prop="Name" header-align="left" align="left" min-width="200" label="名称" />
      <el-table-column prop="Code" header-align="left" align="left" min-width="80" label="代码" />
      <el-table-column fixed="right" header-align="center" align="center" width="160" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="addOrUpdateHandle(scope.row.Id)">
            <i class="el-icon--right el-icon-edit" />&nbsp;修改
          </el-button>
          <el-button type="text" @click="deleteHandle(scope.row.Id)">
            <i class="el-icon--right el-icon-delete" />&nbsp;删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row justify="end">
      <el-col style="text-align: right;">
        <el-pagination
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :page-count="totalPage"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
        />
      </el-col>
    </el-row>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" />
  </div>
</template>
<script>
import AddOrUpdate from './tenant-add-or-update'
import { fetchList, deleteTenant, batchDelete } from '@/api/tenant'
export default {
  components: {
    AddOrUpdate
  },
  data() {
    return {
      addOrUpdateVisible: false,
      viewHeight: this.$store.state.common.documentClientHeight - 210,
      dataListLoading: false,
      dataForm: {
        IndexCondition: ''
      },
      total: 1,
      totalPage: 1,
      pageSize: 10,
      pageIndex: 1,
      dataList: [],
      multipleSelection: []
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    getDataList() {
      this.dataListLoading = true
      const condition = {
        limit: this.pageSize,
        offset: this.pageSize * (this.pageIndex - 1)
      }
      if (this.dataForm.IndexCondition) {
        condition.query = `Code.contains:${this.dataForm.IndexCondition},Condition_Join:or,Name.contains:${this.dataForm.IndexCondition}`
      }
      fetchList(condition).then((data) => {
        this.dataList = data.data
        this.totalPage = data.count % this.pageSize === 0 ? (data.count / this.pageSize) : (Math.floor(data.count / this.pageSize) + 1)
        this.total = data.count
        this.dataListLoading = false
      }).catch(() => {
        this.dataListLoading = false
      })
    },
    //
    handleSelectionChange(val) {
      this.multipleSelection = val
    },

    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    batchDeleteHandler() {
      const list = []
      for (const data of this.multipleSelection) {
        list.push(data.Id)
      }
      this.$confirm(`确定对进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchDelete(list).then((data) => {
          debugger
          if (data && data.code === '20000') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    },
    // 删除
    deleteHandle(id) {
      this.$confirm(`确定对[id=${id}]进行[删除]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTenant(id).then((data) => {
          if (data && data.code === '20000') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>
